//
//  MineAllRainViewController.m
//  MJApp
//
//  Created by wangliang on 15/7/5.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "MineAllRainViewController.h"
#import "RainTableViewCell.h"

@interface MineAllRainViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *headBackView;
@property (weak, nonatomic) IBOutlet UIButton *receiveBtn;
@property (weak, nonatomic) IBOutlet UIButton *sendBtn;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *tableArray;

@end

@implementation MineAllRainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self UI];
    [self obationData:@"0"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSMutableArray *)tableArray {
    
    if (!_tableArray) {
        
        _tableArray = [NSMutableArray array];
    }
    return _tableArray;
}


- (void)UI {
    
    self.title = @"评论";
    [self.rightBtn setTitle:@"清空全部" forState:UIControlStateNormal];
    self.view.backgroundColor = Color_Back_Gray;
    
    self.headBackView.layer.borderColor = [UIColor grayColor].CGColor;
    self.headBackView.layer.borderWidth = 1;
    self.headBackView.layer.cornerRadius = 3;
    
    [self.receiveBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    [self.receiveBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
    [self.receiveBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.receiveBtn.selected = YES;
    [self.sendBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    [self.sendBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.sendBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
    
    [self setBtn:self.receiveBtn];
    [self setBtn:self.sendBtn];
    [self.sendBtn addTarget:self action:@selector(headSendBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.receiveBtn addTarget:self action:@selector(headReceiveBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
}

- (void)setBtn:(UIButton *)btn {
    
    if (btn.selected) {
        
        btn.backgroundColor = [UIColor whiteColor];
    }
    else {
        
        btn.backgroundColor = RGB(200, 200, 200);
    }
}

- (void)headSendBtn:(UIButton *)sender {
    
    if (!self.sendBtn.selected) {
        
        self.receiveBtn.selected = NO;
        self.sendBtn.selected = YES;
        [self setBtn:self.receiveBtn];
        [self setBtn:self.sendBtn];
        [self obationData:@"0"];
    }
}

- (void)headReceiveBtn:(UIButton *)sender {
    
    if (!self.receiveBtn.selected) {
        
        self.receiveBtn.selected = YES;
        self.sendBtn.selected = NO;
        [self setBtn:self.receiveBtn];
        [self setBtn:self.sendBtn];
        [self obationData:@"1"];
    }
}

- (void)obationData:(NSString *)type {
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"user_id"] = [userInfoTool share].ID;
    dic[@"type"] = type;
    [WLHttpRequestAfter userGetCenterDiscussWithParam:dic usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
        
        
        if ([resultDictionary[@"status"] integerValue] == 1) {
            
            [self.tableArray removeAllObjects];
            [self.tableArray addObjectsFromArray:resultDictionary[@"data"]];
            [self.tableView reloadData];
        }
        
    } andFailureBlock:^(NSError *resultError) {
        
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.tableArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    RainTableViewCell *cell = [RainTableViewCell initWithTableView:tableView];
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:self.tableArray[indexPath.row]];
    dic[@"id"] = @"allRain";
    cell.infoDic = dic;
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    RainTableViewCell *cell = (RainTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    
    return cell.cellHigh;
}

@end
